import java.lang.reflect.Array;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-03-20
 * Time: 12:51
 */
class Alg<T extends Comparable<T>>{
    //泛型方法
    public <T extends Comparable<T>>T findMax(T[] array){
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(max.compareTo(array[i])<0){
                max = array[i];
            }
        }
        return max;
    }
}

class Alg2{
    //泛型静态方法
    public static <T extends Comparable<T>>T findMax(T[] array){
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(max.compareTo(array[i])<0){
                max = array[i];
            }
        }
        return max;
    }
}

class MyArrayList<T  extends Number> {
//    public T[] elem;
//    public int usedSize;

//    public MyArrayList(Class<T> clazz, int capacity) {
//        this.elem = (T[]) Array.newInstance(clazz, capacity);
//    }
//    public MyArrayList(int capacity) {
//        this.elem = (T[]) new Object[capacity];
//    }
}

class Alg3{
    //使用泛型打印
    public static <T> void print1(ArrayList<T> list){
        for (T x: list) {
            System.out.println(x);
        }
    }
    //使用统配符打印
    public static void print2(ArrayList<?> list){
        for (Object x: list) {
            System.out.println(x);
        }
    }
    public static void printAll(ArrayList<? extends Number> list) {
        for (Number n: list) {
            System.out.println(n);
        }
    }


    public static void print(ArrayList<? super Number> list){
        for (Object s:list) {  //只能使用Object接收，因为传入的类是Number或者是Number的父类
            System.out.println(s);
        }
    }
    public static void main(String[] args) {
         print(new ArrayList<Number>());
        print(new ArrayList<Object>());
//        print(new ArrayList<Double>());//error
//        print(new ArrayList<String>());//error
//        print(new ArrayList<Integer>());//error
    }


    public static void main4(String[] args) {
//        printAll(new ArrayList<String>());
    }
}


class Animal{}
class Cat extends Animal{}
class Dog extends Animal{}


class Person{}
class Student extends Person{}

public class Test {
    public static void main(String[] args) {
        ArrayList<? super Person> arrayList = new ArrayList<Person>();
        arrayList.add(new Person());
        arrayList.add(new Student());
        Object o = arrayList.get(0); //在获取数据时只能使用Object类型的变量接收

        //因为构造对象时可以构造Person父类类型的arrayList，取出的对象不一定是Person或者Person的子类
//        Person p = arrayList.get(0);
//        Student s = arrayList.get(0);
    }


    public static void main6(String[] args) {
        ArrayList<? super Person> arrayList = new ArrayList<Person>();
       // ArrayList<? super Person> arrayList1 = new ArrayList<Student>(); //error arrayList1 只能引用Person或Person的父类类型
        //添加的元素是 person 或 person 的子类
        arrayList.add(new Person());
        arrayList.add(new Student());
    }



    //泛型
    public static <T extends Animal> void print1(ArrayList<T>list){
        for (T s:list) {
            System.out.println(s);
        }
    }
    //通配符
    public static void print2(ArrayList<? extends Animal> list){
        for (Animal s:list) {
            System.out.println(s);
        }
    }



    public static void main5(String[] args) {
        Cat cat = new Cat();
        Dog dog = new Dog();

        ArrayList<Cat> list1 = new ArrayList<>();
        ArrayList<Dog> list2 = new ArrayList<>();
        list1.add(cat);
        list2.add(dog);
        print2(list1);
        print2(list2);
    }
    public static void main3(String[] args) {
        Alg<Integer> alg1 = new Alg<>();
        Alg<String> alg2 = new Alg<>();
        System.out.println(alg1);
        System.out.println(alg2);
    }

    public static void main2(String[] args) {
        Alg<Integer> alg1 = new Alg<>();
        Integer[] arr1 = {1,12,6,3};
        int max = alg1.findMax(arr1);
        System.out.println(alg1.findMax(arr1));
        Alg<String> alg2 = new Alg<>();
        String[] str = {"hello","abc","qwer"};
        String ret = alg2.findMax(str);
        System.out.println(alg2.findMax(str));

    }


    public static void main1(String[] args) {
        MyArrayList<Integer> myArrayList1 = new MyArrayList<>();
        MyArrayList<Float> myArrayList2 = new MyArrayList<>();
        MyArrayList<Number> myArrayList3 = new MyArrayList<>();

//        MyArrayList<String> myArrayList4 = new MyArrayList<>();
    }
}



